package dev.marsh.sniper.core;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;

public class SniperView extends RelativeLayout {
    private static final String TAG = "SniperView";

    protected JavaCameraView mPreview;
    protected ScanBoxView mScanBoxView;
    protected boolean mSpotAble = false;

    public SniperView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public SniperView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        mPreview = new JavaCameraView(context, attrs);
        mPreview.setId(R.id.sniper_camera_preview);
        addView(mPreview);

        mScanBoxView = new ScanBoxView(context);
        mScanBoxView.initCustomAttrs(context, attrs);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(context, attrs);
        layoutParams.addRule(RelativeLayout.ALIGN_TOP, mPreview.getId());
        layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, mPreview.getId());
        addView(mScanBoxView, layoutParams);
    }

    /**
     * 设置扫描图像的代理
     *
     * @param delegate 扫描图像的代理
     */
    public void setDelegate(CameraBridgeViewBase.CvCameraViewListener2 delegate) {
        if (mPreview != null) {
            mPreview.setCvCameraViewListener(delegate);
        }
    }

    public ScanBoxView getScanBoxView() {
        return mScanBoxView;
    }

    /**
     * 显示扫描框
     */
    public void showScanRect() {
        if (mScanBoxView != null) {
            mScanBoxView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏扫描框
     */
    public void hiddenScanRect() {
        if (mScanBoxView != null) {
            mScanBoxView.setVisibility(View.GONE);
        }
    }

    /**
     * 启动预览、显示扫描框
     */
    public void startSniper() {
        try {
            showScanRect();
            mPreview.enableView();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭预览、隐藏扫描框
     */
    public void stopSniper() {
        try {
            hiddenScanRect();
            mPreview.disableView();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 开始识别
     */
    public void startSpot() {
        mSpotAble = true;
        mPreview.startSpot();
    }

    /**
     * 停止识别
     */
    public void stopSpot() {
        mSpotAble = false;
        mPreview.stopSpot();
    }

    /**
     * 停止识别，并且隐藏扫描框
     */
    public void stopSpotAndHiddenRect() {
        stopSpot();
        hiddenScanRect();
    }

    /**
     * 显示扫描框，并且开始识别
     */
    public void startSpotAndShowRect() {
        startSpot();
        showScanRect();
    }

    /**
     * 销毁扫描控件
     */
    public void onDestroy() {
        stopSniper();
    }
}